package com.car.order.controller;

import com.car.commons.core.model.Result;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import com.car.order.entity.form.shoppingCar.ShoppingCarChangeForm;
import org.springframework.web.bind.annotation.*;
import com.car.order.entity.ShoppingCar;
import com.car.order.service.ShoppingCarService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PutMapping;
import java.util.List;

/**
 *
 *
 * @author 杜岷芫
 * @since 2024-09-05 18:59:29
 */
@RestController
@RequestMapping("/api/shoppingCar")
@Api(tags = " ")
public class ShoppingCarController {

    @Autowired
    private ShoppingCarService shoppingCarService;

    /**
     * 列表查询
     *
     * @param userId
     * @return
     */
    @ApiOperation(value = "查询购物车")
    @PostMapping("/findList/{userId}")
    public Result findList(@PathVariable("userId") Integer userId) throws Exception {
        List<ShoppingCar> result = shoppingCarService.findList(userId);
        return Result.getResult().setMsg("查询成功").setData(result);
    }


    /**
     * 新增
     *
     * @param form
     * @return
     */
    @ApiOperation(value = "加入购物车")
    @PostMapping
    public Result insert( @RequestBody com.car.order.form.shoppingCar.ShoppingCarInsertForm form) throws Exception {
        ShoppingCar shoppingCar = shoppingCarService.insert(form);
        return Result.getResult().setMsg("操作成功").setData(shoppingCar);
    }


    /**
     * 数量直接改变
     *
     * @param form
     * @return
     */
    @ApiOperation(value = "数量改变")
    @PutMapping("/change")
    public Result change(@RequestBody ShoppingCarChangeForm form) throws Exception {
        ShoppingCar shoppingCar = shoppingCarService.change(form);
        return Result.getResult().setMsg("操作成功").setData(shoppingCar);
    }



    /**
     * 删除
     *
     * @param userId
     * @param goodsId
     * @return
     */
    @ApiOperation(value = "删除")
    @DeleteMapping("/{userId}/{goodsId}")
    public Result delete(@PathVariable("userId") Integer userId,@PathVariable("goodsId") Integer goodsId) throws Exception {
        shoppingCarService.delete(userId,goodsId);
        return Result.getResult().setMsg("操作成功");
    }

}